package com.jiashihui.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiashihui.web.controller.swagger2.DeptApi;
import com.jiashihui.web.model.Dept;
import com.jiashihui.web.model.base.BaseResponse;
import com.jiashihui.web.model.base.ResultObjectResponse;
import com.jiashihui.web.service.IDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jiashihui
 * @since 2019-01-29
 */
@Slf4j
@RestController
@RequestMapping("dept")
public class DeptController implements DeptApi {

    @Autowired
    private IDeptService service;

    /**
     * @author: jiashihui
     * @description 部门表添加数据
     * @date: 2019/1/29 16:41
     */
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public BaseResponse save(@RequestBody Dept dept) {
        BaseResponse response = new BaseResponse();
        boolean result = service.save(dept);
        response.setCode("200");
        response.setMsg("操作成功！");
        if (!result) {
            response.setCode("-200");
            response.setMsg("操作失败！");
        }
        return response;
    }

    /**
     * @author: jiashihui
     * @description 根据部门名称获取部门信息
     * @date: 2019/1/30 15:59
     */
    @GetMapping(value = "/get/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public ResultObjectResponse get(@PathVariable Integer id) {
        ResultObjectResponse response = new ResultObjectResponse();
        try {
            response.setObject(service.getById(id));
            response.setCode("200");
            response.setMsg("操作成功！");
            log.debug("test debug");
        } catch (Exception e) {
            response.setCode("-200");
            response.setMsg("失败原因：" + e.getStackTrace());
        }
        return response;
    }

    /**
     * @author: jiashihui
     * @description 分页获取全部部门信息
     * @date: 2019/1/30 15:59
     */
    @GetMapping(value = "/getList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public ResultObjectResponse getList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        ResultObjectResponse response = new ResultObjectResponse();
        try {
            Page<Dept> page = new Page<Dept>(pageNum, pageSize);
            QueryWrapper<Dept> deptQuery = new QueryWrapper<Dept>(new Dept());
            IPage<Dept> pages = service.page(page, deptQuery);
            response.setObject(pages.getRecords());
            response.setCode("200");
            response.setMsg("操作成功！");
        } catch (Exception e) {
            response.setCode("-200");
            response.setMsg("失败原因：" + e.getStackTrace());
        }
        return response;
    }

    /**
     * @author: jiashihui
     * @description 获取符合条件的部门信息
     * @date: 2019/1/29 16:41
     */
    @PostMapping(value = "/queryUserList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @Override
    public ResultObjectResponse queryUserList(@RequestBody Dept dept) {
        ResultObjectResponse response = new ResultObjectResponse();
        try {
            response.setObject(service.queryUserList(dept));
            response.setCode("200");
            response.setMsg("操作成功！");
        } catch (Exception e) {
            response.setCode("-200");
            response.setMsg("失败原因：" + e.getMessage());
        }
        return response;
    }

}

